﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Watson.Core.Tools;
using ITOMMS.Entity;
using ITOMMS.Business;

namespace ITOMMS.Web.InsideManage
{
    public partial class GoodsInAdd : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DropDownListHelper.InitDropList<tbSupplierEntity>(new tbSupplierEntity().GetAll(), ddl供应商, "ID", "公司名称");


                if (Request.Params["id"] != null)
                {
                    string sid = Request.Params["id"].ToString();
                    int pid = 0;
                    if (int.TryParse(sid, out pid))
                    {
                        DataShow(pid);
                    }
                }


                if (Request.Params["vid"] != null)
                {
                    string sid = Request.Params["vid"].ToString();
                    int pid = 0;
                    if (int.TryParse(sid, out pid))
                    {
                        DataShow(pid);
                        btnSave.Visible = false;
                    }
                }

            }
        }


        private void DataShow(int troubleId)
        {
            tbPurchaseInfoEntity entity = new tbPurchaseInfoEntity();
            entity = entity.GetById(troubleId);

            if (entity != null)
            {
                ViewState["Entity"] = entity;

                txt名称.Text = entity.名称;
                txt编号.Text = entity.编号;
                this.ddl供应商.SelectedValue = entity.供应商.ToString();
                txt品牌.Text = entity.品牌;
                txt类别.Text = entity.类别;
                txt单位.Text = entity.单位;
                txt型号.Text = entity.型号;
                txt规格.Text = entity.规格;
                this.txt送货人.Text = entity.送货人;
                this.txt数量.Text = entity.数量;


                txt采购价.Text = entity.采购价.ToString();
                txt批发价.Text = entity.批发价.ToString();
                txt零售价.Text = entity.零售价.ToString();
                if (entity.进货时间.HasValue)
                {
                    txt进货时间.Text = entity.进货时间.Value.ToShortDateString();
                }

                txt备注.Text = entity.备注;
                if (entity.是否包含税.Value)
                {
                    RadioButton1.Checked = true;
                }
                else
                {
                    RadioButton2.Checked = true;
                }

            }
        }



        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                Entity.tbPurchaseInfoEntity entity = new ITOMMS.Entity.tbPurchaseInfoEntity();
                if (ViewState["Entity"] != null)
                {
                    entity = ViewState["Entity"] as tbPurchaseInfoEntity;
                }
                else
                {
                    entity.CreateBy = SessionHelper.UserID;
                    entity.CreateDate = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                }



                entity.名称 = txt名称.Text;
                entity.编号 = txt编号.Text;
                entity.供应商 = int.Parse(this.ddl供应商.SelectedValue);
                entity.品牌 = txt品牌.Text.Trim();
                entity.类别 = txt类别.Text.Trim();
                entity.单位 = txt单位.Text.Trim();
                entity.型号 = txt型号.Text;
                entity.规格 = txt规格.Text;
                entity.送货人 = txt送货人.Text;
                entity.数量 = txt数量.Text;

                if (txt采购价.Text.Trim() != string.Empty)
                {
                    entity.采购价 = decimal.Parse(txt采购价.Text.ToString());
                }

                if (txt批发价.Text.Trim() != string.Empty)
                {
                    entity.批发价 = decimal.Parse(txt批发价.Text.ToString());
                }
                if (txt零售价.Text.Trim() != string.Empty)
                {
                    entity.零售价 = decimal.Parse(txt零售价.Text.ToString());
                }

                DateTime purtime = new DateTime();
                if (DateTime.TryParse(txt进货时间.Text.Trim(), out  purtime))
                {
                    entity.进货时间 = purtime;
                }


                entity.备注 = txt备注.Text;
                if (RadioButton1.Checked)
                {
                    entity.是否包含税 = false;
                }
                else
                {
                    entity.是否包含税 = true;
                }


                entity.ModifyBy = SessionHelper.UserID;
                entity.ModifyDate = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));


                entity.SaveOrUpdate(entity);
                ViewState["Entity"] = entity;
                MessageboxInfo.MessageBox(this, MessageType.SUCCEEDED, "保存成功");
            }
            catch (Exception ex)
            {
                log4netHelper.error(ex);
                MessageboxInfo.MessageBox(this, MessageType.ERROR, "保存失败");
            }
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("GoodsInList.aspx", true);
        }
    }
}
